package replication;

import beans.*;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import context.ContextManager;
import context.Files;


import javolution.util.FastList;

import beans.Information;

public class ReplicationReceiver extends Thread{

	public static Logger log = Logger.getLogger("log_file");
	 FileHandler hand;
	
	ContextManager cm;
	
	private FastList<Serializable>FileBA;
	private FastList<Serializable>FileLV;
	private FastList<Serializable>FileMY;
	private FastList<Serializable>FileNC;
	
	int queueNumber=1;
	int port=9999;
	
	public ReplicationReceiver() {
		// TODO Auto-generated constructor stub
		FileBA=new FastList<Serializable>();
		FileLV=new FastList<Serializable>();
		FileMY=new FastList<Serializable>();
		FileNC=new FastList<Serializable>();
		this.cm=null;
		//methodeServer();
	}
	
	public ReplicationReceiver(ContextManager cm) {
		// TODO Auto-generated constructor stub
		FileBA=new FastList<Serializable>();
		FileLV=new FastList<Serializable>();
		FileMY=new FastList<Serializable>();
		FileNC=new FastList<Serializable>();
		this.cm=cm;
		//methodeServer();
	}
	
	public void run(){
		try {
			hand = new FileHandler("MonLog.txt");
			log.addHandler(hand);
			SimpleFormatter formatter=new SimpleFormatter();
			hand.setFormatter(formatter);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		receiveReplication();
	}

	public void receiveReplication(){
		try {
			ServerSocket server_socket = null;
		    Socket socket = null;
		      server_socket = new ServerSocket(port);
		 
		      while (true) {
		       // System.out.println("Wait for replicated context ....");
		        socket = server_socket.accept();
		      //  System.out.println("Accepted from " + socket.getInetAddress());
		      
		        ObjectInputStream oi =
		                   new ObjectInputStream(socket.getInputStream());
		 
		      //Read serialized object
		      //Information info = (Information) oi.readObject();
		        Object o=oi.readObject();
		        
		        testObjectReceived(o);
		      }		 
		     
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testObjectReceived(Object o){
		   if (o instanceof NewContext) {
				System.out.println("=========> C'est un Nouveau Contexte");
				log.info("=========> C'est un Nouveau Contexte");
				//Seter les files pleines puis faire new ..
				FileBA=new FastList<Serializable>();
				FileLV=new FastList<Serializable>();
				FileMY=new FastList<Serializable>();
				FileNC=new FastList<Serializable>();				
			}
	        
	        if (o instanceof Information) {
				Information info = (Information) o;
				System.out.println("=> C'est une information => "+info.toString());
				log.info("=> C'est une information => "+info.toString());
				insertInformation(info);		
			}
	        
	        if (o instanceof NextQueue) {
				System.out.println("===> C'est un nouvelle queue !!!!!!!");
				log.info("===> C'est un nouvelle queue !!!!!!!");
				queueNumber++;
			}		        
	        
	        if(o instanceof EndContext){
	        	System.out.println("=======> Fin du context !!!!!!!");
	        	log.info("=======> Fin du context !!!!!!!");
	        	print();
	        	//Met à jour le contexte
	        	//cm.SetContext(FileBA,FileLV,FileMY,FileNC);
	        }
	}
	
	void insertInformation(Information info){
		switch (queueNumber) {
		case 1:
				FileBA.add(info);
			break;
			
		case 2:
				FileMY.add(info);
			break;
			
		case 3:
				FileLV.add(info);
			break;
			
		case 4:
				FileNC.add(info);
			break;

		default:
			break;
		}
	}
	
	public void print(){
		log.info("FileBA : "+FileBA.size());
		System.out.println("FileBA : "+FileBA.size());
		log.info("FileMY : "+FileMY.size());
		System.out.println("FileMY : "+FileMY.size());
		log.info("FileLV : "+FileLV.size());
		System.out.println("FileLV : "+FileLV.size());
		log.info("FileLV : "+FileLV.size());
		System.out.println("FileNC : "+FileNC.size());
	}

	public FastList<Serializable> getFileBA() {
		return FileBA;
	}

	public void setFileBA(FastList<Serializable> fileBA) {
		FileBA = fileBA;
	}

	public FastList<Serializable> getFileLV() {
		return FileLV;
	}

	public void setFileLV(FastList<Serializable> fileLV) {
		FileLV = fileLV;
	}

	public FastList<Serializable> getFileMY() {
		return FileMY;
	}

	public void setFileMY(FastList<Serializable> fileMY) {
		FileMY = fileMY;
	}

	public FastList<Serializable> getFileNC() {
		return FileNC;
	}

	public void setFileNC(FastList<Serializable> fileNC) {
		FileNC = fileNC;
	}

	public int getQueueNumber() {
		return queueNumber;
	}

	public void setQueueNumber(int queueNumber) {
		this.queueNumber = queueNumber;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
}
